package com.yanqu.road.server.logic.recharge.limit.activity;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.mall.LimitBuyResult;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroupUnion;
import com.yanqu.road.exception.BusinessException;
import com.yanqu.road.server.gameplayer.module.activity.fund.ActivityFundModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;

public class HsIslandZlMallLimit extends BaseActivityMallLimit{

    public HsIslandZlMallLimit(long userId, MallInfo mallInfo, ActivityInfo activityInfo) {
        super(userId, mallInfo, activityInfo);
    }

    @Override
    protected int activityCheck() {
        if(!ActivityMgr.activityInShowTime(activityInfo)){
            return GameErrorCode.E_ACTIVITY_NO_IN_TIME_CANNOT_BUY;
        }
        if(activityInfo.getType() != eActivityType.HsIsland.getValue()){
            return GameErrorCode.E_ACTIVITY_NO_IN_TIME_CANNOT_BUY;
        }
        //分组结束后才可以购买
        List<Integer> paramList = StringUtils.stringToIntegerList(activityInfo.getUnionGroup(), "\\|");
        if (paramList.size() < 4) {
            return GameErrorCode.E_ACTIVITY_NO_IN_TIME_CANNOT_BUY;
        }
        int groupTime = paramList.get(3);
        long groupEndTime = this.activityInfo.getBeginTime() + groupTime * DateHelper.MINUTE_SECONDS;
        if (groupEndTime > (System.currentTimeMillis() / 1000)) {
            return GameErrorCode.E_ACTIVITY_NO_IN_TIME_CANNOT_BUY;
        }
        //没分组资格不能购买
        UnionActivityGroupUnion unionData = GameUnionActivityGroupMgr.getUnionData(activityInfo.getActivityId(), player.getUserId());
        if (unionData == null) {
            return GameErrorCode.E_HS_ISLAND_NOT_IN_LIST;
        }
        return 0;
    }

    @Override
    protected boolean isSystemUnLock() {
        return SystemOpenMgr.systemOpen(player, eSystemId.HsIsland.getValue());
    }

    @Override
    protected LimitBuyResult buyCountLimit() {
        return activityMallCountLimit(player);
    }

    @Override
    protected int logicCheck() {
        boolean buyToken = player.getModule(ActivityFundModule.class).isBuy(mallInfo);
        if (buyToken) {
            return GameErrorCode.E_MALL_LIMIT_BUY;
        }
        return 0;
    }
}
